package com.zl.tby.controller;

import com.zl.tby.core.base.bean.OptionType;
import com.zl.tby.core.base.util.EasyExcelUtils;
import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyMountainsPersonQuery;
import com.zl.tby.dto.TbyVillageInfoQuery;
import com.zl.tby.model.TbyVillageInfo;
import com.zl.tby.service.ITbyVillageInfoService;
import com.zl.tby.vo.TbyVillageInfoForm;
import com.zl.tby.vo.exp.TbyMountainsPersonExportExcel;
import com.zl.tby.vo.exp.TbyVillageInfoExportExcel;
import org.noear.solon.annotation.Body;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.validation.annotation.NotNull;
import org.noear.solon.validation.annotation.Valid;
import org.noear.solon.validation.annotation.Validated;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 社区共管 - 村信息 控制器
 * @author sunjiaxing
 * @version 1.0
 * @since 2024/8/22 00:32
 */
@Valid
@Controller
@Mapping("/village_info")
public class TbyVillageInfoController {

    @Inject
    private ITbyVillageInfoService tbyVillageInfoService;

    /**
     * 保存或新增
     * @param form: 表单对象
     * @return 操作结果
     */
    @Mapping(value = "/save", method = MethodType.POST)
    public R<TbyVillageInfo> saveOrUpdate(@Validated TbyVillageInfoForm form) {
        try {
            if (form.getId() == null) {
                return tbyVillageInfoService.add(form);
            }
            return tbyVillageInfoService.update(form);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.failed(ex.getMessage());
        }
    }

    /**
     * 删除数据
     * @param id: 主键id
     * @return 结果
     */
    @Mapping(value = "/delete", method = MethodType.DELETE)
    public R<Void> delete(@NotNull Long id) {
        return this.tbyVillageInfoService.delete(id);
    }

    /**
     * 根据id查询
     * @param id: 主键id
     * @return 结果
     */
    @Mapping(value = "/get", method = MethodType.GET)
    public R<TbyVillageInfo> get(@NotNull Long id) {
        TbyVillageInfo entity = this.tbyVillageInfoService.get(id);
        return entity == null ? R.failed("数据不存在") : R.ok(entity);
    }

    /**
     * 分页查询
     * @param query: 查询参数
     * @return 分页结果
     */
    @Mapping(value = "/page", method = MethodType.GET)
    public R<?> page(@Body TbyVillageInfoQuery query) {
        return R.ok(this.tbyVillageInfoService.page(query));
    }

    /**
     * 列表查询
     * @param query: 查询参数
     * @return 列表结果
     */
    @Mapping(value = "/list", method = MethodType.GET)
    public R<List<TbyVillageInfo>> List(@Body TbyVillageInfoQuery query) {
        return R.ok(this.tbyVillageInfoService.list(query));
    }

    /**
     * 列表查询不含内容
     * @param query: 查询参数
     * @return 列表结果
     */
    @Mapping(value = "/list_no_content", method = MethodType.GET)
    public R<List<TbyVillageInfo>> listNoContent(@Body TbyVillageInfoQuery query) {
        return R.ok(this.tbyVillageInfoService.listNoContent(query));
    }

    /**
     * 下拉选数据
     * @return 结果
     */
    @Mapping(value = "/options", method = MethodType.GET)
    public R<List<OptionType>> options(@Body TbyVillageInfoQuery query) {
        return R.ok(this.tbyVillageInfoService.options(query));
    }

    /**
     * 导出Excel
     * @param query: 查询条件
     * @param ctx：上下文
     * @throws IOException: 异常
     */
    @Mapping(value = "/excel", method = MethodType.POST)
    public void excel(@Body TbyVillageInfoQuery query, Context ctx) throws IOException {
        List<TbyVillageInfoExportExcel> dataList = TbyVillageInfoExportExcel.createList(this.tbyVillageInfoService.list(query));
        EasyExcelUtils.exportExcel(ctx, dataList, TbyVillageInfoExportExcel.class, "村庄信息", "村庄信息");
    }
}
